package reflecdome;

import java.lang.reflect.*;

/**
 * Created with IntelliJ IDEA.
 * Description: 反射
 * User: 王圆豪
 * Date: 2024-10-30
 * Time: 23:08
 */

class Student{
    public String name = "张三";
    private int age = 18;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student(String name) {
        this.name = name;
    }

    public Student(){

    }

    private void printName(){
        System.out.println(this.name);
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
//        //反射私有方法
//        Class<?> classStudent = Class.forName("reflecdome.Student");
//        Method method = classStudent.getDeclaredMethod("printName");
//        method.setAccessible(true);
//        Student student = (Student) classStudent.newInstance();
//        method.invoke(student);



//        //反射私有属性
//        Class<?> classStudent = Class.forName("reflecdome.Student");
//        Field field = classStudent.getDeclaredField("name");
//        field.setAccessible(true);
//        Student student = (Student) classStudent.newInstance();
//        field.set(student,"bite");
//        System.out.println(field.get(student));

        ////反射私有的构造方法 屏蔽内容为获得公有的构造方法
//        Class<?> classStudent = Class.forName("reflecdome.Student");
//        Constructor<?> constructor = classStudent.getDeclaredConstructor(String.class, int.class);
////        Constructor<?> constructor1 = classStudent.getDeclaredConstructor();

//        //设置为true后可修改访问权限
//        constructor.setAccessible(true);
//        Student student = (Student) constructor.newInstance("bite", 18);
////        Student student = (Student) constructor.newInstance();
//        System.out.println(student);

        //创建对象
//        try{
//            //根据类名返回类的对象
//            Class<?> classStudent = Class.forName("reflecdome.Student");
//            //创建类的实例
//            Student student = (Student) classStudent.newInstance();
//
//        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
//            throw new RuntimeException(e);
//        }


//        try {
//            Class<?> classStudent = Class.forName("reflecdome.Student");
//            Class[] classes = classStudent.getDeclaredClasses();
//            System.out.println(classes.toString());
//            ClassLoader classLoader = classStudent.getClassLoader();
//            System.out.println(classLoader.toString());
//            System.out.println(classStudent.toString());
//            Student student = (Student) classStudent.newInstance();
//            System.out.println(student.toString());
//            student.name = "bite";
//            System.out.println(student.toString());
//            Class<?> l = classStudent.getDeclaringClass();
//            System.out.println(classStudent.getName());
//        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
//            throw new RuntimeException(e);
//        }
    }
}
